PM2 + New Next.js Project (TypeScript + TSX)
Concise, step-by-step instructions to install requirements, create a new Next.js (TypeScript) project, run it in development, build for production, start with PM2, and update PM2 after new builds. Follow in order on a Debian/Ubuntu Linux server.
1) Install system packages and Node.js (recommended)
Run these commands first.
Option A — Recommended: install Node using nvm (flexible, per-user, easy upgrades):
sudo apt update
sudo apt install -y curl git build-essential
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash
source ~/.bashrc
# Inspect available Node versions, then install the one you want.
nvm list-remote
# Example: install the latest LTS (recommended) or install a specific version from the list:
nvm install --lts
# Or: nvm install <VERSION_FROM_LIST>
# Verify
node -v
npm -v
Option B — Alternative: NodeSource (system-wide package, less flexible than nvm):
sudo apt update
sudo apt install -y curl git build-essential
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt install -y nodejs
# Verify
node -v
npm -v
(Recommendation: use nvm on servers where you control a deploy user — it avoids global npm permission issues and makes upgrades easier.)
2) Install PM2 globally
sudo npm install -g pm2
pm2 -v
3) Create the Next.js project (choose folder where you want the repo)
Change to the directory where you want the new project folder created, then run npx:
cd /path/where/you/want/projects
npx create-next-app@latest my-next-app --typescript
# When prompted, choose defaults or options you prefer.
cd my-next-app
npm install
4) Run development server
npm run dev
# Dev server available at http://localhost:3000
Stop dev when ready to build (Ctrl+C).
5) Build for production
npm run build
# Optional: run locally to test the production build
npm start # starts production Next server on default port 3000
6) Start the production app with PM2
Start using the npm start script (recommended for Next.js server):
pm2 start "npm start" --name my-next-app
pm2 save
Confirm status and logs:
pm2 status
pm2 logs my-next-app --lines 200
7) Make PM2 restore processes on reboot
Generate and run the startup command PM2 prints, then save:
pm2 startup
# Follow/execute the printed sudo command (example:)
# sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u deploy --hp /home/deploy
pm2 save
Run the printed command using the user you want PM2 to run under (avoid running as root).
8) Update after changes / redeploy workflow
Typical update steps on the server after you pull changes or copy a new build:
cd /path/to/my-next-app
git pull origin main # or upload new code/build from CI
npm ci # or npm install
npm run build
# If app uses npm start (Node server)
pm2 restart my-next-app
# If you changed pm2 config or added/removed processes:
pm2 save
Alternative: if you prefer a zero-downtime reload (clustered apps), use:
pm2 reload my-next-app
9) Useful PM2 commands (cheatsheet)
- List processes: pm2 status
- View logs: pm2 logs name
- Tail logs: pm2 logs my-next-app --lines 200
- Restart app: pm2 restart my-next-app
- Reload zero-downtime: pm2 reload my-next-app
- Stop / delete: pm2 stop my-next-app ; pm2 delete my-next-app
- Save current process list: pm2 save
- Show info: pm2 describe my-next-app
10) Notes & tips
- Run PM2 under a non-root deploy user. Use
pm2 startupoutput to configure systemd for that user. - Keep secrets out of repo; use environment variables. Consider
ecosystem.config.jsfor env per app. - If using Nginx as reverse proxy, proxy to localhost:3000 (or your configured PORT).
- Test the production build locally (
npm start) before putting it behind PM2/Nginx. - Consider CI to build artifacts and rsync/upload them to the server, then run the update commands above.